package cn.rdq.iknessplatform.controller;

import cn.rdq.api.po.Catelog;
import cn.rdq.api.vo.CatelogMapVo;
import cn.rdq.api.vo.CatelogVo;
import cn.rdq.common.CommonResult;
import cn.rdq.iknessplatform.service.CatelogCollectService;
import cn.rdq.iknessplatform.service.CatelogService;
import cn.rdq.iknessplatform.service.UserPursueService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/catelog")
public class CatelogController {

    @Autowired
    private CatelogService catelogService;

    @Autowired
    private UserPursueService userPursueService;

    @Autowired
    private CatelogCollectService collectService;


    @PostMapping("/{userId}")
    public CommonResult catelogIndex(@PathVariable Long userId, @RequestBody CatelogMapVo catelogMapVo) {
        Map<String, Object> map = new HashMap<>();
        map.put("pursuePage", new PageInfo(userPursueService.userPurseByUserId(catelogMapVo.getPursueVo())));
        map.put("watchedPage", new PageInfo(userPursueService.userWatchedByUserId(catelogMapVo.getWatchedVo())));
        map.put("catelogPage", new PageInfo(catelogService.pageByUserId(catelogMapVo.getCatelogVo())));
        map.put("collectPage", new PageInfo(collectService.listInfoByUserId(catelogMapVo.getCatelogCollectedVo())));
        return CommonResult.successObj(map);
    }

    @GetMapping("/user/{userId}")
    public CommonResult catelogByUser(@PathVariable Long userId) {
        List<Catelog> catelogs = catelogService.listByUserId(userId);
        return CommonResult.successObj(catelogs);
    }

    @PostMapping("/pageUser")
    public CommonResult catelogPageUser(@RequestBody CatelogVo catelogVo) {
        List<Catelog> catelogs = catelogService.pageByUserId(catelogVo);
        return CommonResult.success().pageSuccess(catelogs);
    }

    @PostMapping("/createDefault")
    public CommonResult createDefault(@RequestBody Catelog catelog) {
        catelogService.createDefaultCatelog(catelog);
        return CommonResult.success();
    }

    @PostMapping("/save")
    public CommonResult save(@RequestBody Catelog catelog) {
        catelogService.save(catelog);
        return CommonResult.success();
    }

    @GetMapping("/del/{id}")
    public CommonResult deleteById(@PathVariable Long id) {
        catelogService.deleteCatelogById(id);
        return CommonResult.success();
    }

    @GetMapping("/list")
    public CommonResult list(CatelogVo catelogVo) {
        List<Catelog> catelogs = catelogService.catelogList(catelogVo);
        return CommonResult.success().pageSuccess(catelogs);
    }

    @PostMapping("/update")
    public CommonResult update(@RequestBody Catelog catelog) {
        return CommonResult.successObj(catelogService.updateCatelogIntro(catelog));
    }
}
